﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using MS.Enums;

namespace MS.Utility.EnumHelper {
	public class EnumHelper {
		private static readonly Hashtable ht = Hashtable.Synchronized(new Hashtable());

		/// <summary>
		/// 从缓存中获取枚举对应的List
		/// </summary>
		/// <param name="type">类型</param>
		/// <returns></returns>
		public static IList<KeyValuePair<string, string>> GetCachedDictionary(Type type) {
			if (ht.Contains(type)) {
				return ht[type] as IList<KeyValuePair<string, string>>;
			} else {
				IList<KeyValuePair<string, string>> dict = GetDictionary(type);
				ht[type] = dict;
				return dict;
			}
		}

		/// <summary>
		/// 获取枚举对应的List
		/// </summary>
		/// <param name="type">类型</param>
		/// <returns></returns>
		public static IList<KeyValuePair<string, string>> GetDictionary(Type type) {
			if (!type.IsEnum) {
				throw new InvalidOperationException("错误的枚举类型");
			}
			FieldInfo[] fields = type.GetFields();
			IList<KeyValuePair<string, string>> dict = new List<KeyValuePair<string, string>>();
			foreach (var item in fields) {
				if (item.FieldType.IsEnum == false) { continue; }
				string desription = string.Empty;
				object[] objs = item.GetCustomAttributes(typeof(DescriptionAttribute), false);
				if (objs != null && objs.Length > 0) {
					DescriptionAttribute da = (DescriptionAttribute)objs[0];
					desription = da.Description;
				} else {
					desription = item.Name;
				}
				dict.Add(new KeyValuePair<string, string>(((int)Enum.Parse(type, item.Name)).ToString(), desription));
			}
			return dict;
		}

		/// <summary>
		/// 根据值从缓存中获取枚举描述
		/// </summary>
		/// <param name="type">类型</param>
		/// <param name="fieldName">枚举值</param>
		/// <returns></returns>
		public static string GetCachedDescription(Type type, string fieldName) {
			if (string.IsNullOrEmpty(fieldName)) return string.Empty;
			IList<KeyValuePair<string, string>> dictionary = GetCachedDictionary(type);
			foreach (KeyValuePair<string, string> keyValuePair in dictionary) {
				if (keyValuePair.Key == ((int)(Enum.Parse(type, fieldName))).ToString()) {
					return keyValuePair.Value;
				}
			}
			return fieldName;
		}

		/// <summary>
		/// 根据描述获取枚举值
		/// </summary>
		/// <param name="type">类型</param>
		/// <param name="description">描述</param>
		/// <returns></returns>
		public static string GetCachedFieldName(Type type, string description) {
			IList<KeyValuePair<string, string>> dictionary = GetCachedDictionary(type);
			foreach (KeyValuePair<string, string> keyValuePair in dictionary) {
				if (keyValuePair.Value == description) {
					return keyValuePair.Key;
				}
			}
			return description;
		}

		///<summary>
		/// 根据枚举类型获取描述
		///</summary>
		///<param name="value">类型</param>
		///<returns>描述</returns>
		public static string GetEnumDescription(Enum value) {
			FieldInfo fi = value.GetType().GetField(value.ToString());
			DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
			if (attributes.Length > 0) {
				return attributes[0].Description;
			} else {
				return value.ToString();
			}
		}

		/// <summary>
		/// 根据枚举值获取枚举
		/// </summary>
		/// <param name="type">类型</param>
		/// <param name="fieldName">枚举值</param>
		/// <returns></returns>
		public static KeyValuePair<string, string> GetEnum(Type type, string fieldName) {
			KeyValuePair<string, string> kvp = new KeyValuePair<string, string>();
			IList<KeyValuePair<string, string>> dictionary = GetCachedDictionary(type);
			foreach (KeyValuePair<string, string> keyValuePair in dictionary) {
				if (keyValuePair.Key == ((int)(Enum.Parse(type, fieldName))).ToString()) {
					kvp = keyValuePair;
					break;
				}
			}
			return kvp;
		}
	 
		 
	}
}
